
var FeedbackBlock = function(headline,type,questions){
	console.log('Headline:'+headline+'Type:'+type+'Questions:'+questions);
	this.headline = headline;
	this.type=type;
	this.questions = questions;
	this.answers = new Array();
}

FeedbackBlock.prototype.printHeader = function(){
		var row = "<div class=\"ui-grid-a\">"+
				"<div class=\"ui-block-a\">"+
					"<label for=\"name\">"+this.headline+"</label>"+
				"</div>";
		return row;
}

FeedbackBlock.prototype.printRateQuestions = function(){
		var rows = new String();
		console.log(this.questions);
	 	$.each(this.questions,function(index,question){
	 		rows += "<div class=\"ui-grid-a feedback-question\">"+
				"<div class=\"ui-block-a\">"+
					"<label for=\"name\">"+question+"</label>"+
				"</div>"+
				"<div class=\"ui-block-b\"><span class=\"star\" id=\"star"+index+"\"></span></div>"+
			"</div>";
			//rows.append(row);
	 	});
		console.log("ROWS:"+rows);
		return rows;
}

FeedbackBlock.prototype.printCommentQuestions = function(){
		var rows = new String();
		console.log(this.questions);
	 	$.each(this.questions,function(index,question){
	 		rows += "<div class=\"ui-grid-a feedback-question\">"+
				"<div class=\"ui-block-a\">"+
					"<label for=\"name\">"+question+"</label>"+
				"</div>"+
				"<div class=\"ui-block-b\"><textarea name=\"textarea\"  id=\"textarea"+index+"\"></textarea></div>"+
			"</div>";
			//rows.append(row);
	 	});
		
		return rows;
}

FeedbackBlock.prototype.print = function(){
	if (this.type =='options'){
		return this.printRateQuestions();
	}
	else{
		return this.printCommentQuestions();
	}
}

FeedbackBlock.prototype.updateOptionsModel = function(){
	for (var i=0;i<this.questions.length;i++){
		var stars = $('#star'+i).raty('score');
		var valueObj = new Object();
		valueObj[i]=stars;
		this.answers.push(stars);
		//this.questions[i].wert = stars;
		//console.log(this.questions[i].wert);
	}
}

FeedbackBlock.prototype.updateCommentModel = function(){
	for (var i=0;i<this.questions.length;i++){
		var content =  $('#textarea'+i)[0].value;
		var valueObj = new Object();
		valueObj[i]=content;
		this.answers.push(valueObj);
		console.log(content);
		//this[i].wert =content;
	}
	
}

FeedbackBlock.prototype.updateModelValue = function(){
	if (this.type == 'options'){
		this.updateOptionsModel();
	}
	else{
		this.updateCommentModel();
	}
}




var FeedbackModel = function(){
//	console.log('Headline:'+headline+'Questions:'+questions);
//	this.headline = headline;
	//this.questions = questions;
	this.feedbackElementList = new Array();
}

FeedbackModel.prototype.addElement = function(feedbackElement){
	this.feedbackElementList.push(feedbackElement);
}

FeedbackModel.prototype.rate = function (id,stars) {
	$.each(this.questions,function(index){
		this.questions[id].stars=$('#star'+index).raty('score');
	}); 
}


FeedbackModel.prototype.printQuestions = function(){
	var questions=new String();
	$.each(this.feedbackElementList,function(){
		questions+=this.print();
	})
	return questions;
}




FeedbackModel.prototype.save = function(){
		var feedbackPostUrlPath=new String();
		var dataToSend = new String();
		for (var i=0;i< this.feedbackElementList.length;i++){
			feedbackBlock = this.feedbackElementList[i];
			feedbackBlock.updateModelValue();
			console.log(feedbackBlock.answers);
			dataToSend = JSON.stringify(feedbackBlock.answers);
			//dataToSend =feedbackBlock.answers;
			if (debug){
				console.log("JSON");
				console.log(dataToSend);
				console.log("===");
			}
			if (feedbackBlock.type == 'options'){
				feedbackPostUrlPath="/feedbackOptions";
			}
			else{
				feedbackPostUrlPath="/feedbackComment";
			}
			this.postMethod(feedbackPostUrlPath,dataToSend);
		}
}

FeedbackModel.prototype.postMethod = function(feedbackPostUrlPath,dataToSend){
		if (mocked){
					  $.ajax({
						    url: "http://localhost:8080"+feedbackPostUrlPath,
						    type: "POST",
						    data: dataToSend,
						     contentType: 'application/json',
						     crossDomain: true,
						     success:function(){
						    	$( "#popupBasic" ).popup("open");
						    },
						    error:function(){
						    	$( "#popupBasicError" ).popup("open");
						    },
						    dataType: "json"
						});
						 }
					else{
						console.log(JSON.stringify(this.questions));


						$.ajax({
						    url: "http://localhost:8080"+feedbackPostUrlPath,
						    type: "POST",
						     contentType: 'application/json',
						    data: dataToSend,
						    success:function(){
						    	$( "#popupBasic" ).popup("open");
						    },
						    error:function(){
						    	$( "#popupBasicError" ).popup("open");
						    },
						    dataType: "json"
						});
						}

}

FeedbackModel.prototype.udpate = function(){
		if (mocked){
							$.ajax({ 
							   type: "PUT",
							   dataType: "json",
							   url: "/data/feedback.json",
							   success: function(data){  
							   	$.each(data,function(data){
		   							console.log(data); 

		   								});
							   },
							   error: function (xhr, status,errorThrown) {
							   	console.log("ERROR:"+errorThrown);
							   }

							});
						 }
					else{
						console.log(JSON.stringify(this.questions));
						$.ajax({
						    url: "http://localhost:8080/feedback",
						    type: 'PUT',
						    contentType: 'application/json',
						    data: JSON.stringify(this.questions),
						    dataType: 'json'
						});
						}
}



var FeedbackController = function (theModel) {  

  feedback.model = theModel;
  feedback.feedbackModel = new FeedbackModel();
};

FeedbackController.prototype.model = function(){
	return feedback.feedbackModel;
}

FeedbackController.prototype.loadModelData = function () {
 this.model = new FeedbackModel(['Was ist das?','Warum?','Wie gehts?'],"");
}

FeedbackController.prototype.save = function () {
  				feedback.feedbackModel.save();
}

FeedbackController.prototype.load = function(){
		if (mocked){
							$.ajax({ 
							   type: "GET",
							   dataType: "json",
							   async:false,
							   url: "/data/feedback.json",
							   success: function(data){  
							   	$.each(data,function(section,feedbackObj,questions){
							   		console.log(section);
		   							console.log(feedbackObj.headline);
		   							console.log("questions"+feedbackObj.questions);
		   							var questions = new Array();
		   							feedbackElement = new FeedbackBlock(feedbackObj.headline,
		   												feedbackObj.type,
		   												feedbackObj.questions);	
		   							feedback.feedbackModel.addElement(feedbackElement);
		   								});
							   },
							   error: function (xhr, status,errorThrown) {
							   	alert("error");
							   	console.log("ERROR:"+errorThrown);
							   }

							});
						 }
					else{
						$.ajax({type: "GET",
							   dataType: "jsonp",
							   url: "http://localhost:8080/feedback",
							   success: function(data){        
							     $.each(data,function(data){
		   							console.log(data); 
		   								});
							   },
							   error: function (xhr, status) {
							   	console.log("ERROR:"+errorThrown);
							   }

							});
						}
}

